package com.example.hospitalapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class AppointmentAdapter extends BaseAdapter {

    private Context context;
    private List<Appointment> appointments;

    public AppointmentAdapter(Context context, List<Appointment> appointments) {
        this.context = context;
        this.appointments = appointments;
    }

    @Override
    public int getCount() {
        return appointments.size();
    }

    @Override
    public Object getItem(int position) {
        return appointments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return appointments.get(position).getAppointmentId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_appointment, parent, false);
        }

        Appointment appointment = appointments.get(position);

        TextView txtAppointmentTime = convertView.findViewById(R.id.txtAppointmentTime);
        TextView txtDoctorName = convertView.findViewById(R.id.txtDoctorName);
        TextView txtSpecialty = convertView.findViewById(R.id.txtSpecialty);
        TextView txtStatus = convertView.findViewById(R.id.txtStatus);
        TextView txtPaymentStatus = convertView.findViewById(R.id.txtPaymentStatus);
        TextView txtQueueNumber = convertView.findViewById(R.id.txtQueueNumber);
        TextView txtQueueTime = convertView.findViewById(R.id.txtQueueTime);
        Button btnPay = convertView.findViewById(R.id.btnPay);
        Button btnQueueStatus = convertView.findViewById(R.id.btnQueueStatus); // 新增按钮

        // 设置预约信息
        txtAppointmentTime.setText("预约时间: " + appointment.getAppointmentTime());
        txtDoctorName.setText("医生: " + appointment.getDoctorName());
        txtSpecialty.setText("科室: " + appointment.getSpecialty());
        txtStatus.setText("状态: " + appointment.getStatus());
        txtPaymentStatus.setText("支付状态: " + appointment.getPaymentStatus());

        // 如果支付状态是未付款，显示缴费按钮
        if ("未付款".equals(appointment.getPaymentStatus())) {
            btnPay.setVisibility(View.VISIBLE);
            btnPay.setOnClickListener(v -> {
                // 点击缴费按钮，跳转到支付界面
                Intent intent = new Intent(context, PaymentActivity.class);
                intent.putExtra("appointmentId", appointment.getAppointmentId());
                ((Activity) context).startActivityForResult(intent, 1001); // 1001 是一个请求码
            });
        } else {
            btnPay.setVisibility(View.GONE);
        }

        // 显示排队信息（如果已设置）
        if (appointment.getQueueNumber() != 0) {
            txtQueueNumber.setText("排队号: " + appointment.getQueueNumber());
            txtQueueTime.setText("排队时间: " + appointment.getQueueTime());
            txtQueueNumber.setVisibility(View.VISIBLE);
            txtQueueTime.setVisibility(View.VISIBLE);
        } else {
            txtQueueNumber.setVisibility(View.GONE);
            txtQueueTime.setVisibility(View.GONE);
        }

        // 点击查看排队按钮，查询排队信息
        btnQueueStatus.setOnClickListener(view -> {
            // 执行查询排队信息的逻辑
            String appointmentId = String.valueOf(appointment.getAppointmentId());
            queryQueueStatus(appointmentId, txtQueueNumber, txtQueueTime);
        });

        return convertView;
    }

    private void queryQueueStatus(String appointmentId, TextView txtQueueNumber, TextView txtQueueTime) {
        // 查询排队信息的逻辑
        ApiClient apiClient = new ApiClient(context);
        JSONObject params = new JSONObject();
        try {
            params.put("appointmentId", appointmentId); // 将 appointmentId 作为请求体发送
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 调用 POST 请求
        apiClient.postRequest("http://zhsj.autumnclouds.cn/queue/query", params, new ApiClient.VolleyCallback() {
            @Override
            public void onSuccess(JSONObject response) {
                try {
                    int code = response.getInt("code");
                    if (code == 200) {
                        // 处理排队信息并更新视图
                        JSONArray data = response.getJSONArray("data");
                        if (data.length() > 0) {
                            JSONObject queueData = data.getJSONObject(0);  // 假设获取第一个排队记录
                            int queueNumber = queueData.getInt("queueNumber");
                            String queueTime = queueData.getString("queueTime");

                            // 更新排队号和排队时间显示
                            txtQueueNumber.setText("排队号: " + queueNumber);
                            txtQueueTime.setText("排队时间: " + queueTime);

                            // 显示排队号和排队时间
                            txtQueueNumber.setVisibility(View.VISIBLE);
                            txtQueueTime.setVisibility(View.VISIBLE);

                            Toast.makeText(context, "排队信息查询成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(context, "未找到排队信息", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(context, "查询排队失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(context, "查询排队信息失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(String error) {
                Toast.makeText(context, "排队查询失败: " + error, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
